﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for LectureHandler
    /// </summary>
    public class eng_LectureHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Lecture     
            string add_lecture = context.Request["add_lecture"];
            if (!string.IsNullOrEmpty(add_lecture))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_lecture>(add_lecture);
                bussiness.InsertAllInOne<eng_lecture_excercise>(obj.excercise_array_id, "excercise_id", "lecture_id", obj.lecture_id);
               context.Response.Write("Success Full");
            }
            string view_lecture = context.Request["view_lecture"];
            if (!string.IsNullOrEmpty(view_lecture))
            {
                string Id = context.Request["Id"];
                RenderSingleLecture(context, Convert.ToInt32(Id));
            }
            string list_lecture = context.Request["list_lecture"];
            if(!string.IsNullOrEmpty(list_lecture)){
                RenderLecture(context);
            }
            string delete_lecture = context.Request["delete_lecture"];
            if (!string.IsNullOrEmpty(delete_lecture)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_lecture>(delete_lecture);
                RenderLecture(context);
            }
            string lockunlock_lecture = context.Request["lockunlock_lecture"];
            if (!string.IsNullOrEmpty(lockunlock_lecture)) {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               bussiness.LockUnLock<eng_lecture>(lockunlock_lecture, "lecture_active");
                RenderLecture(context);
            }
            #endregion

            #region Lecture Level
            string add_lecture_level = context.Request["add_lecture_level"];
            if (!string.IsNullOrEmpty(add_lecture_level))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               var obj = bussiness.InsertOrUpdate<eng_lectures_level>(add_lecture_level);
               context.Response.Write("Success Full");
                
            }
            string view_lecture_level = context.Request["view_lecture_level"];
            if (!string.IsNullOrEmpty(view_lecture_level))
            {
                string Id = context.Request["Id"];
                RenderSingleLectureLevel(context, Convert.ToInt32(Id));
            }
            string list_lecture_level = context.Request["list_lecture_level"];
            if (!string.IsNullOrEmpty(list_lecture_level))
            {
                RenderLectureLevel(context);
            }
            string delete_lecture_level = context.Request["delete_lecture_level"];
            if (!string.IsNullOrEmpty(delete_lecture_level))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_lectures_level>(delete_lecture_level);
                RenderLectureLevel(context);
            }
            string lockunlock_lecture_level = context.Request["lockunlock_lecture_level"];
            if (!string.IsNullOrEmpty(lockunlock_lecture_level))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               bussiness.LockUnLock<eng_lectures_level>(lockunlock_lecture_level, "level_lecture_active");
                RenderLectureLevel(context);
            }
            #endregion

            #region Lecture Table
            string add_lecture_table = context.Request["add_lecture_table"];
            if (!string.IsNullOrEmpty(add_lecture_table))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               var obj = bussiness.InsertOrUpdate<eng_lectures_table>(add_lecture_table);
               context.Response.Write("Success Full");
            }
            string view_lecture_table = context.Request["view_lecture_table"];
            if (!string.IsNullOrEmpty(view_lecture_table))
            {
                string Id = context.Request["Id"];
                RenderSingleLectureTable(context, Convert.ToInt32(Id));
            }
            string list_lecture_table = context.Request["list_lecture_table"];
            if (!string.IsNullOrEmpty(list_lecture_table))
            {
                RenderLectureTable(context);
            }
            string delete_lecture_table = context.Request["delete_lecture_table"];
            if (!string.IsNullOrEmpty(delete_lecture_table))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               bussiness.Delete<eng_lectures_table>(delete_lecture_table);
                RenderLectureTable(context);
            }
            string lockunlock_lecture_table = context.Request["lockunlock_lecture_table"];
            if (!string.IsNullOrEmpty(lockunlock_lecture_table))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_lectures_table>(lockunlock_lecture_table, "table_lecture_active");
                RenderLectureTable(context);
            }
            #endregion

            #region Lecture Chapter
            string add_lecture_chapter = context.Request["add_lecture_chapter"];
            if (!string.IsNullOrEmpty(add_lecture_chapter))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               var obj = bussiness.InsertOrUpdate<eng_lectures_chapter>(add_lecture_chapter);
               context.Response.Write("Success Full");
            }
            string view_lecture_chapter = context.Request["view_lecture_chapter"];
            if (!string.IsNullOrEmpty(view_lecture_chapter))
            {
                string Id = context.Request["Id"];
                RenderSingleLectureChapter(context, Convert.ToInt32(Id));
            }
            string list_lecture_chapter = context.Request["list_lecture_chapter"];
            if (!string.IsNullOrEmpty(list_lecture_chapter))
            {
                RenderLectureChapter(context);
            }
            string delete_lecture_chapter = context.Request["delete_lecture_chapter"];
            if (!string.IsNullOrEmpty(delete_lecture_chapter))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_lectures_chapter>(delete_lecture_chapter);
                RenderLectureChapter(context);
            }
            string lockunlock_lecture_chapter = context.Request["lockunlock_lecture_chapter"];
            if (!string.IsNullOrEmpty(lockunlock_lecture_chapter))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_lectures_chapter>(lockunlock_lecture_chapter, "chapter_lecture_active");
                RenderLectureChapter(context);
            }
            #endregion
        }
        private void RenderSingleLecture(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_lecture>(
               c => c.lecture_id.Equals(Id),
               c => new
               {
                   id = c.lecture_id,
                  chapter_id=c.chapter_lecture_id,
                   title = c.lecture_title,
                   contentenglish=c.lecture_content_english,
                   contentvietnam=c.lecture_content_vietnam,
                   contentnote=c.lecture_content_note,
                   file=c.lecture_file,
                   active = c.lecture_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderLecture(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_lecture>(
                null,
                null,
                null,
                c => c.lecture_title,
                 c => new
                 {
                     id = c.lecture_id,
                     chapter_id = c.chapter_lecture_id,
                     title = c.lecture_title,
                     contentenglish = c.lecture_content_english,
                     contentvietnam = c.lecture_content_vietnam,
                     contentnote = c.lecture_content_note,
                     file = c.lecture_file,
                     active = c.lecture_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        private void RenderSingleLectureLevel(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_lectures_level>(
               c => c.level_lecture_id.Equals(Id),
               c => new
               {
                   id = c.level_lecture_id,
                   courseid = c.course_id,
                   title = c.level_lecture_title,
                   content=c.level_lecture_content,
                   active = c.level_lecture_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderLectureLevel(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_lectures_level>(
                null,
                null,
                null,
                c => c.level_lecture_title,
                c => new
                {
                    id = c.level_lecture_id,
                    courseid = c.course_id,
                    title = c.level_lecture_title,
                    content = c.level_lecture_content,
                    active = c.level_lecture_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        private void RenderSingleLectureTable(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_lectures_table>(
                c => c.table_lecture_id.Equals(Id),
               c => new
               {
                   id = c.table_lecture_id,
                   levelid = c.level_lecture_id,
                   typecode=c.type_code,
                   title = c.table_lecture_title,
                   content = c.table_lecture_content,
                   active = c.table_lecture_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderLectureTable(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_lectures_table>(
                null,
                null,
                null,
                c => c.table_lecture_title,
                 c => new
                 {
                     id = c.table_lecture_id,
                     levelid = c.level_lecture_id,
                     typecode = c.type_code,
                     title = c.table_lecture_title,
                     content = c.table_lecture_content,
                     active = c.table_lecture_active
                 });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        private void RenderSingleLectureChapter(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_lectures_chapter>(
               c => c.chapter_lecture_id.Equals(Id),
               c => new
               {
                   id = c.chapter_lecture_id,
                   tableid = c.table_lecture_id,
                   title = c.chapter_lecture_title,
                   content=c.chapter_lecture_content,
                   active = c.chapter_lecture_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderLectureChapter(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
             IList<object> list = layer.List<eng_lectures_chapter>(
                 null,
                 null,
                 null,
                 c => c.chapter_lecture_title,
                 c => new
               {
                   id = c.chapter_lecture_id,
                   tableid = c.table_lecture_id,
                   title = c.chapter_lecture_title,
                   content=c.chapter_lecture_content,
                   active = c.chapter_lecture_active
               });
             string json = serializer.Serialize(list);
             context.Request.ContentType = "aplication/json";
             context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}